package pl.edu.agh.neuraleconomy.persistence.exchange;

import java.sql.SQLException;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import pl.edu.agh.neuraleconomy.common.exception.ApplicationRuntimeException;
import pl.edu.agh.neuraleconomy.model.exchange.Exchange;
import pl.edu.agh.neuraleconomy.persistence.base.BaseDao;

public class ExchangeDao extends BaseDao<Exchange> {

	public ExchangeDao() {
		super(Exchange.class);
	}

	public List<Exchange> getByCompany(Long companyId) {
		try {
			return dao.queryBuilder().orderBy(Exchange.DATE, true).where().eq(Exchange.COMPANY_ID, companyId).query();
		} catch (SQLException e) {
			throw new ApplicationRuntimeException(e);
		}
	}
	
	public List<Exchange> getByDate(Date date) {
		try {
			return dao.queryBuilder().where().eq(Exchange.DATE, date).query();
		} catch (SQLException e) {
			throw new ApplicationRuntimeException(e);
		}
	}
	
	public Exchange getByCompanyAndDate(Long companyId, Date date) {
		try {
			List<Exchange> found = dao.queryBuilder().where().eq(Exchange.COMPANY_ID, companyId).and().eq(Exchange.DATE, date).query(); 
			return found.isEmpty() ? null : found.get(0);
		} catch (SQLException e) {
			throw new ApplicationRuntimeException(e);
		}
	}

	public List<Exchange> getByCompanyBetweenDates(Long companyId, Date from, Date to) {
		try {
			return dao.queryBuilder().orderBy(Exchange.DATE, true).where().eq(Exchange.COMPANY_ID, companyId).and()
					.between(Exchange.DATE, from, to).query();
		} catch (SQLException e) {
			throw new ApplicationRuntimeException(e);
		}
	}

	public Exchange getLatestByCompanyBeforeDate(Long companyId, Date date){
		try {
			List<Exchange> foundExchanges = dao.queryBuilder().orderBy(Exchange.DATE, false).where().eq(Exchange.COMPANY_ID, companyId).and().lt(Exchange.DATE, date).query();
			if(!foundExchanges.isEmpty()){
				return foundExchanges.get(0);
			}
			return null;
		} catch (SQLException e){
			throw new ApplicationRuntimeException(e);
		}
	}
	
	public List<Exchange> getLatestByCompanyIncludeDate(Long companyId, Date date, Long number){
		try {
			List<Exchange> res = dao.queryBuilder().orderBy(Exchange.DATE, false).limit(number).where().eq(Exchange.COMPANY_ID, companyId).and().le(Exchange.DATE, date).query();
			Collections.reverse(res);
			return res;
		} catch (SQLException e){
			throw new ApplicationRuntimeException(e);
		}
	}
	
	public List<Exchange> getLatestByCompanyExcludeDate(Long companyId, Date date, Long number){
		try {
			List<Exchange> res = dao.queryBuilder().orderBy(Exchange.DATE, false).limit(number).where().eq(Exchange.COMPANY_ID, companyId).and().lt(Exchange.DATE, date).query();
			Collections.reverse(res);
			return res;
		} catch (SQLException e){
			throw new ApplicationRuntimeException(e);
		}
	}
	
	public Date getNewestDate(){
		try{
			List<Exchange> list = dao.queryBuilder().orderBy(Exchange.DATE, false).limit(1L).query();
			if(!list.isEmpty()){
				return list.get(0).getDate();
			}
			return null;
		}catch(SQLException e){
			throw new ApplicationRuntimeException(e);
		}
	}
}
